package com.sun.wanandroid

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.core.view.WindowCompat
import androidx.lifecycle.lifecycleScope
import com.sun.module_base.model.AppThemeState
import com.sun.module_base.network.manager.NetworkStateManager
import com.sun.module_base.network.manager.NetworkType
import com.sun.module_base.theme.SystemUiController
import com.sun.module_base.widget.BaseScreen
import com.sun.module_network.utils.logi
import com.sun.wanandroid.ui.main.MainScreen
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    private var networkJob: Job? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            val systemUiController = remember { SystemUiController(window) }
            val appTheme = remember { mutableStateOf(AppThemeState()) }
            BaseScreen(appTheme.value, systemUiController) {
//                OptimizedStatusBar(themeColor)
                MainScreen(appTheme)
            }
        }


        networkJob = lifecycleScope.launch {
            NetworkStateManager.getInstance(this@MainActivity).mNetworkState.collect {
                when (it) {
                    is NetworkStateManager.NetworkStatus.Lost -> {
                        "无网络连接".logi()
                    }

                    is NetworkStateManager.NetworkStatus.Changed -> {
                        "网络连接状态变化".logi()
                        updateNetWorkType(it.type)
                    }

                    is NetworkStateManager.NetworkStatus.Available -> {
                        "网络已连接".logi()
                    }
                }
            }
        }
    }
//
//    private fun setStatus() {
//        val decorView = window.decorView
//
//        val controller = WindowInsetsControllerCompat(window, decorView)
//        window.statusBarColor = Color.Transparent.toArgb()
//        controller.isAppearanceLightStatusBars = true
//        controller.set
//
//        decorView.viewTreeObserver.addOnWindowFocusChangeListener { hasFocus ->
//            if (hasFocus) {
//                controller.isAppearanceLightStatusBars = !isDarkTheme()
//            }
//        }
//    }
//
//    private fun isDarkTheme(): Boolean {
//        return resources.configuration.uiMode and android.content.res.Configuration.UI_MODE_NIGHT_MASK == android.content.res.Configuration.UI_MODE_NIGHT_YES
//    }

    private fun updateNetWorkType(type: NetworkType) {
        val netText = when (type) {
            NetworkType.WIFI -> "wifi网络"
            NetworkType.CELLULAR -> "移动网络"
            NetworkType.ETHERNET -> "有线网络"
            NetworkType.UNKNOWN -> "未知网络"
        }
        "当前网络状态为：$netText".logi()
    }

    override fun onDestroy() {
        super.onDestroy()
        networkJob?.cancel()
        NetworkStateManager.getInstance(this).unregister()
    }
}